//产品数量加减
$(function () {
    var pro_my = $(".pro_value1").html();//读取商品单价
    var pro_my1 = parseFloat(pro_my);
    var n = $(".order_nm").find("input").val();//读取商品数量input中的值
    var n1 = parseFloat(n);
    var p_v = n1 * pro_my1;	 //显示商品价格

    var yf = $(".freight_my2").html();//读取运费价格
    var yf1 = parseFloat(yf);

    if (p_v < 168) {
        yf1 = 5;
    } else {
        yf1 = 0;
    }
    flag = $(".card input[type='checkbox']").is(':checked');
    var all_p = p_v + yf1;//商品价格加上邮费
    var now = $('#now').val();
    if (now == '1') {
        $(".pro_money").html(p_v);
        if (p_v >= 168) {
            all_p = p_v;
            $('.freight_my2').text('包邮')
            $('#shippingPrice').val('0');
        } else {
            $('.freight_my2').text('￥5.00');
            $('#shippingPrice').val('5');
        }

        if (p_v >= 99) {
            $('#surprice').text('满99元送一朵微情书（春风款）')
        } else {
            $('#surprice').text('商品金额满99元有惊喜哦~');
        }

        if (flag) {
            all_p = Number(all_p) + Number(9.9)
        }
        $(".pro_money2").html(all_p);
    }

    //减法
    $(".minus").click(function () {
        var now = $('#now').val();
        var nm = $(this).siblings(".order_nm").find("input").val();//读取input中的值
        var nm1 = parseInt(nm);
        var cart_id = $(this).attr('cart_id');
        if (nm1 <= 1) {
            $(this).siblings(".order_nm").find("input").html(1);
            var p_my = pro_my1;
            var p_my2 = p_my;//总价
        } else {
            var nm2 = nm1 - 1;
            //console.log(nm1);
            var p_my = nm2 * pro_my1;
            var p_my2 = p_my;//总价
            $(this).siblings(".order_nm").find("input").val(nm2);

            $(".product_nm").html(nm2);
        }
        // $('#voucherTitle').text('请点击选取你的优惠券')
        // $('#voucherId') .val(-1);
        if (now == '1') {
            $(".pro_money").html(p_my.toFixed(2));
            if (p_my >= 168) {
                p_my2 = p_my;
                $('.freight_my2').text('包邮')
                $('#shippingPrice').val('0');
            } else {
                p_my2 = Number(p_my2) + Number(5)
                $('.freight_my2').text('￥5.00');
                $('#shippingPrice').val('5');
            }

            if (p_my >= 99) {
                $('#surprice').text('满99元送一朵微情书（春风款）')
            } else {
                $('#surprice').text('商品金额满99元有惊喜哦~');
            }

            flag = $(".card input[type='checkbox']").is(':checked')
            if (flag) {
                p_my2 = Number(p_my2) + Number(9.9)
            }
            $(".pro_money2").html(p_my2.toFixed(2));
        } else {
            $.ajax({
                type: "POST",
                url: "/index.php?m=Home&c=Cart&a=changeGoodNum",
                data: {'cart_id': cart_id, 'goods_num': nm2},
                success: function (response) {
                    var dataObj = eval("(" + response + ")");
                    $('.pro_money').text(dataObj.allMoney)
                    flag = $(".card input[type='checkbox']").is(':checked')
                    if (flag) {
                        dataObj.allMoneyShipping = Number(dataObj.allMoneyShipping) + Number(9.9)
                    }
                    $('.pro_money2').text(dataObj.allMoneyShipping)
                    $('.product_nm_order').text(dataObj.goodNum)
                    $('#postage').val(dataObj.postage)
                    if (dataObj.postage > 1) {
                        $('.freight_my2').text("￥" + dataObj.postage)
                    } else {
                        $('.freight_my2').text('包邮')

                    }

                    if (dataObj.allMoney >= 99) {
                        $('#surprice').text('满99元送一朵微情书（春风款）');
                    } else {
                        $('#surprice').text('商品金额满99元有惊喜哦~');
                    }

                }
            })
        }

    });

    //加法
    $(".add").click(function () {
        var now = $('#now').val();
        var nm = $(this).siblings(".order_nm").find("input").val();//读取input中的值
        var nm1 = parseInt(nm);
        var nm2 = 1 + nm1;
        var p_my = nm2 * pro_my1;
        var p_my2 = p_my;//总价
        var cart_id = $(this).attr('cart_id');
        var specNum = $('#specNum').val();
        if (nm >= Number(specNum)) {
            return;
        }
        // $('#voucherTitle').text('请点击选取你的优惠券')
        // $('#voucherId') .val(-1);
        flag = $(".card input[type='checkbox']").is(':checked');
        $(this).siblings(".order_nm").find("input").val(nm2);
        // $(".pro_money").html(p_my);
        // $(".pro_money2").html(p_my2);
        $(".product_nm").html(nm2);
        if (now == '1') {
            $(".pro_money").html(p_my.toFixed(2));

            if (p_my >= 168) {
                p_my2 = p_my;
                $('.freight_my2').text('包邮')
                $('#shippingPrice').val('0');
            } else {
                $('.freight_my2').text('￥5.00');
                $('#shippingPrice').val('5');
                p_my2 = Number(p_my2) + Number(5)
            }
            if (p_my >= 99) {
                $('#surprice').text('满99元送一朵微情书（春风款）');
            } else {
                $('#surprice').text('商品金额满99元有惊喜哦~');
            }

            if (flag) {
                p_my2 = Number(p_my2) + Number(9.9)
            }
            $(".pro_money2").html(p_my2.toFixed(2))
        } else {
            $.ajax({
                type: "POST",
                url: "/index.php?m=Home&c=Cart&a=changeGoodNum",
                data: {'cart_id': cart_id, 'goods_num': nm2},
                success: function (response) {
                    var dataObj = eval("(" + response + ")");
                    $('.pro_money').text(dataObj.allMoney)
                    flag = $(".card input[type='checkbox']").is(':checked')
                    if (flag) {
                        dataObj.allMoneyShipping = Number(dataObj.allMoneyShipping) + Number(9.9)
                    }
                    $('.pro_money2').text(dataObj.allMoneyShipping)
                    $('.product_nm_order').text(dataObj.goodNum)
                    $('#postage').val(dataObj.postage)
                    if (dataObj.postage > 1) {
                        $('.freight_my2').text(dataObj.postage)
                    } else {
                        $('.freight_my2').text('包邮')

                    }

                    if (dataObj.allMoney>= 99) {
                        $('#surprice').text('满99元送一朵微情书（春风款）');
                    } else {
                        $('#surprice').text('商品金额满99元有惊喜哦~');
                    }
                }
            })
        }
    });

    //有收获地址时，显示绿色支付。
    if ($(".addressee_infor1").is(":hidden")) {
        $(".pay_way1").show();
        $(".pay_way2").hide();
    } else {
        $(".pay_way2").show();
        $(".pay_way1").hide();
    }

    //todo 选择收货地址跳转
    // $(".addr_p").click(function () {
    //     location.href = "../shoppingCar/shopCar.html";
    // });

    //todo 购物车结算
    $(".pay_my").click(function () {
        if ($(".no_border").length == 0) {
            prompt("未选择商品");
            return;
        } else {
            $.ajax({
                type: "POST",
                url: "/index.php?m=Home&c=Cart&a=checkCart",
                success: function (response) {
                    var dataObj = eval("(" + response + ")");
                    console.log(dataObj);
                    if (dataObj.state == 'success') {
                        location.href = "/index.php?m=Home&c=Cart&a=settleAccounts";
                    } else {
                        prompt('商品已提交订单');
                        window.location.reload();
                    }
                }
            })
        }
    });

    //todo orderDetail.html中收货地址跳转-->管理收货地址
    // $(".addressee_infor3 .addr_p").click(function () {
    //     location.href = "../PersonalCenter/chooseReceiveAddress.html";
    // });
    $('.pay_way2').click(function () {
        var info = $('#orderForm').serialize();

    })
});
